//
//  WKPageCell.m
//  CosChat
//
//  Created by Mark on 15/4/27.
//  Copyright (c) 2015年 yq. All rights reserved.
//

#import "WKPageCell.h"
#import "BCStuffSecondTableCell.h"
#import "BCStuffThirdTableCell.h"

@interface WKPageCell ()<UITableViewDelegate,UITableViewDataSource,actionDelegate>
@property (nonatomic ,assign)NSInteger chooseNum;
@property (nonatomic,strong) CALayer *dotLayer;
@property (nonatomic,assign) CGFloat endPointX;
@property (nonatomic,assign) CGFloat endPointY;
@property (nonatomic,strong) UIBezierPath *path;
@end

@implementation WKPageCell
- (id)initWithIdentifier:(NSString *)identifier{
    if (self = [super init]) {
        _identifier = identifier;
        [self createView];
    }
    return self;
}
- (void)createView{
    self.secondListTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 80*wx, SCREEN_HEIGHT-180 *hx) style:UITableViewStylePlain];
    self.secondListTable.backgroundColor = [UIColor colorWithRed:0.949  green:0.949  blue:0.949 alpha:1];
    self.thirdListTable = [[UITableView alloc] initWithFrame:CGRectMake(80 * wx, 0, SCREEN_WIDTH - 80 *wx, SCREEN_HEIGHT-65.f-100.f) style:UITableViewStylePlain];
    self.secondListTable.delegate = self;
    self.secondListTable.dataSource = self;
    [self.secondListTable registerClass:[BCStuffSecondTableCell class] forCellReuseIdentifier:@"secondReuse"];
    self.secondListTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.secondListTable.backgroundColor = [UIColor colorWithRed:0.972  green:0.973  blue:0.972 alpha:1];
    self.secondListTable.showsVerticalScrollIndicator = NO;
    
    UILabel *headLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 80 *wx, 27)];
    headLab.backgroundColor = [UIColor colorWithRed:0.949  green:0.949  blue:0.949 alpha:1];
    headLab.text = @" 标价 : 为邦采推荐价格.";
    headLab.font = [UIFont systemFontOfSize:12];
    headLab.textColor = [UIColor colorWithRed:0.478  green:0.478  blue:0.478 alpha:1];
    
    self.thirdListTable.tableHeaderView = headLab;
    
    self.thirdListTable.delegate = self;
    self.thirdListTable.dataSource = self;
    self.thirdListTable.backgroundColor = [UIColor colorWithRed:0.972  green:0.973  blue:0.972 alpha:1];
    [self.thirdListTable registerClass:[BCStuffThirdTableCell class] forCellReuseIdentifier:@"thirdReuse"];
    [self addSubview:self.secondListTable];
    [self addSubview:self.thirdListTable];
    self.backgroundColor = [UIColor colorWithRed:0.949  green:0.949  blue:0.949 alpha:1];
    _chooseNum = 0;
    
    //分类小红点
    _endPointX = SCREEN_WIDTH*0.02f;
    _endPointY = SCREEN_HEIGHT*0.95f;
    
    
}

- (void)setSecondArr:(NSMutableArray *)secondArr
{
    _secondArr = secondArr;
    [self.secondListTable reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.secondListTable) {
        return self.secondArr.count;
    }else
    {
      
        NSNumber *num = [NSNumber numberWithInteger:self.chooseNum];
        NSNumber *indexNum = [NSNumber numberWithInteger:self.thirdIndex];
        if (self.chooseNum == 1) {
            num = @0;
        }
    NSArray *thirdArr = [BCShopViewManager readSubcategoryDataFromStuff:indexNum Sub:num
                            ];
        self.thirdArr = [NSMutableArray arrayWithArray:thirdArr];
        
        if (self.thirdArr.count <= 0) {
            return 10;
        }else{
        
        return self.thirdArr.count;
        }
    }
}

- (void)setThirdIndex:(NSInteger)thirdIndex
{
    _thirdIndex = thirdIndex;
    [self.thirdListTable reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.secondListTable) {
        return 60;
    }else
    {
        return 118;
    }
}

- (void)pushWithUUID:(NSString *)uuid
{
    
    [self.delegate goThirdWithUUID:uuid];
    [self.thirdListTable reloadData];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.secondListTable) {
        _chooseNum = indexPath.row;
        [self.thirdListTable reloadData];
        [self.secondListTable reloadData];
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.secondListTable) {
        BCStuffSecondTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondReuse"];
        cell.title = self.secondArr[indexPath.row ];
        if (indexPath.row == _chooseNum) {
            cell.choosed = YES;
        }else{
            cell.choosed = NO;
        }
        return cell;

    }else   {
        BCStuffThirdTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"thirdReuse"];
        
        
        __weak __typeof(&*cell)weakCell =cell;
        cell.reddot = ^() {
            CGRect parentRect = [weakCell convertRect:weakCell.plus.frame toView:self];
            [self JoinCartAnimationWithRect:parentRect];
        };
        
        if (self.thirdArr.count > 0) {
            STUFF *stuff = self.thirdArr[indexPath.row];
            cell.name = stuff.name;
            cell.price = stuff.price.floatValue;
            cell.cellStyle = 2;
            cell.detailInfo = stuff.discribtion;
            cell.stuff = stuff;
            cell.stuffImg = [UIImage imageNamed:stuff.icon];
        }else{
        cell.name = [NSString stringWithFormat:@"演示数据"];
        cell.price = 10;
        cell.cellStyle = indexPath.row % 4;
        cell.detailInfo = @"演示数据演示数据演示数据演示数据演示数据演示数据演示数据";
            cell.stuffImg = [UIImage imageNamed:@"占位图"];
        }
        cell.minusHidden = NO;

        cell.amount = 0;
        cell.selectionStyle =   UITableViewCellSelectionStyleNone;
        for (BCShopModel *stuff in [BCShopCarManager sharedInstance].stuffList) {
            if ([cell.name isEqualToString:stuff.name]) {
                cell.minusHidden = YES;
                cell.amount = stuff.amout;
            }
        }
        cell.delegate = self;
        return cell;

      
    }

}

- (void)reloadData
{
    [self.thirdListTable reloadData];
}

#pragma mark -加入购物车动画
-(void) JoinCartAnimationWithRect:(CGRect)rect
{
    
    CGFloat startX = rect.origin.x;
    CGFloat startY = rect.origin.y;
    
    _path= [UIBezierPath bezierPath];
    [_path moveToPoint:CGPointMake(startX, startY)];
    //三点曲线
    [_path addCurveToPoint:CGPointMake(_endPointX, _endPointY)
             controlPoint1:CGPointMake(startX, startY)
             controlPoint2:CGPointMake(startX - 180, startY - 200)];
    
    _dotLayer = [CALayer layer];
    _dotLayer.backgroundColor = [UIColor redColor].CGColor;
    _dotLayer.frame = CGRectMake(0, 0, 15, 15);
    _dotLayer.cornerRadius = (15 + 15) /4;
    [self.layer addSublayer:_dotLayer];
    [self groupAnimation];
    
}
#pragma mark - 组合动画
-(void)groupAnimation
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = _path.CGPath;
    animation.rotationMode = kCAAnimationRotateAuto;
    
    CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"alpha"];
    alphaAnimation.duration = RedAnimationDuring;
    alphaAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    alphaAnimation.toValue = [NSNumber numberWithFloat:0.1];
    alphaAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[animation,alphaAnimation];
    groups.duration = RedAnimationDuring+0.3f;
    groups.removedOnCompletion = NO;
    groups.fillMode = kCAFillModeForwards;
    groups.delegate = self;
    [groups setValue:@"groupsAnimation" forKey:@"animationName"];
    [_dotLayer addAnimation:groups forKey:nil];
    
    [self performSelector:@selector(removeFromLayer:) withObject:_dotLayer afterDelay:RedAnimationDuring+0.3f];
    
}
- (void)removeFromLayer:(CALayer *)layerAnimation{
    
    [layerAnimation removeFromSuperlayer];
}

#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    _reddotAnimationBlock(anim);
}

@end
